/**
 * 
 */
package com.gko100.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.gko100.action.base.AdminBaseAction;
import com.gko100.model.Article;

/**
 * @author Administrator
 *
 */
public class PageListAction extends AdminBaseAction {

	private static int pageSize=10;
	private int totalCount;
	private int pageNo;
	private int pageCount;

	
	public int getPageSize() {
		return pageSize;
	}
	
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	private HttpServletRequest request;
	private List<Article> artiList;

	
	public List<Article> getArtiList() {
		return artiList;
	}

	public void setArtiList(List<Article> artiList) {
		this.artiList = artiList;
	}

	public String find_pageList() throws Exception {
		
		request = ServletActionContext.getRequest();
		if(request.getAttribute("artiList")!=null){
			List<Article> pl = (List<Article>) request.getAttribute("artiList");
			
			this.setTotalCount(pl.size());
			this.setPageCount(this.getTotalCount()/this.getPageSize());
			
			List<Article> al = new ArrayList<Article>();
			if(this.getPageNo()<1){
				this.setPageNo(1);
			}
			if(this.getPageNo()>this.getPageCount()){
				this.setPageNo(this.getPageCount());
			}
			if(this.getPageNo()==1){
				for(int i=0;i<this.getPageSize();i++){
					Article a = pl.get(i);
					if(a!=null){
						al.add(a);
					}
				}
				this.setArtiList(al);
				if(al!=null){
					al.clear();
				}
			}
			if(this.getPageNo()>1&&this.getPageNo()<=this.getPageCount()){
				
				int firstResult = (this.getPageNo()-1)*this.getPageSize();
				for(int i=firstResult;i<firstResult+this.getPageSize()&&i<pl.size();i++){
					
					Article a = pl.get(i);
					if(a!=null){
						al.add(a);
					}
				}
				this.setArtiList(al);
				if(al!=null){
					al.clear();
				}
			}
			
			
			
		}

		return "pageList_find_success";
	}
	
	
}
